import { IDoorCoveringHardwareMode, ISelectParamForm } from '@manycore/pages-vm-api';
import { useState } from 'react';
import { createContainer } from 'unstated-next';

export interface IState {
  /**
   * 是否正在生成
   */
  generating: boolean;
  /**
   * 是否有模型选中
   */
  hasSelected: boolean;
  /**
   * 预览图地址
   */
  sketchImageUrl?: string;
  /**
   * 当前模式
   */
  modeOption?: IDoorCoveringHardwareMode;
  /**
   * 生成场景下的表单
   */
  generateForm: ISelectParamForm[];
  /**
   * 编辑模式下的
   */
  editForm: ISelectParamForm[];
}

const defaultSketchImageUrl = '//qhcmsshape-oss.kujiale.com/profileshape/MGI5RX5MCAD56AABAAAAACI8.png';

function useAppStore(
  initialState: IState = {
    generating: false,
    hasSelected: false,
    modeOption: undefined,
    generateForm: [],
    editForm: [],
    sketchImageUrl: defaultSketchImageUrl,
  }
) {
  const [appState, setAppState] = useState(initialState);

  const updateSelectedModelState = (selected: boolean) => {
    setAppState((state) => {
      return {
        ...state,
        hasSelected: selected,
      };
    });
  };

  const updateAppModeState = (modeOption?: IDoorCoveringHardwareMode) => {
    setAppState((state) => {
      return {
        ...state,
        modeOption,
      };
    });
  };

  const updateGenerateFormState = (generateForm: ISelectParamForm[]) => {
    setAppState((state) => {
      return {
        ...state,
        generateForm,
      };
    });
  };

  const updateGeneratingState = (generating: boolean) => {
    setAppState((state) => {
      return {
        ...state,
        generating,
      };
    });
  };

  const updateSketchImageState = (sketchImageUrl?: string) => {
    setAppState((state) => {
      return {
        ...state,
        sketchImageUrl,
      };
    });
  };

  const updateEditFormState = (editForm: ISelectParamForm[]) => {
    setAppState((state) => {
      return {
        ...state,
        editForm,
      };
    });
  };

  return {
    appState,
    updateSelectedModelState,
    updateAppModeState,
    updateGenerateFormState,
    updateGeneratingState,
    updateSketchImageState,
    updateEditFormState,
  };
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export const AppStore = createContainer(useAppStore);
